<?php

/**
 * @author Home
 * @version 1.0
 * @created 21-9-2013 
 */
include_once "../lib/DAL/DmPhong.php";
include_once "../lib/DAL/connect.php";
class DmPhongBLL{
    var $conn;
    function __construct(){
        $conn = new Connect();
        $conn->cnn();
    }
    
    function __destruct(){
        
    }
    
    public function getPhongInfo($ma_phong){
        $sql="select * from dm_phong where ma_phong = '".$ma_phong."';";        
        $row = mysql_query($sql) or die(mysql_error());        
        return $this->setPhong($row);
    }
    
    public function getPhongTrungTam($ma_trungtam){
        $sql = "select * from dm_phong where ma_trungtam = '".$ma_trungtam."'";
        $result = mysql_query($sql) or die(mysql_error());
        $arr_phong = array();
        $i=0;
        while($row = mysql_fetch_array($result)){
            $arr_phong[$i] = $this->setPhong($row);
            $i++;
        }
        return $arr_phong;
    }
    public function insertPhong($phong){
        $sql="INSERT INTO dm_phong(
                ma_phong
                ,ma_trungtam
                ,sophong
                ,soluongmax
            ) VALUES ('".$phong->getma_phong()
                ."'
                ,'".$phong->getma_trungtam()."' 
                ,'".$phong->getsophong()."' 
                ,".$phong->getsoluongmax().  
            ")";
       $result = mysql_query($sql) or die (mysql_error());
       return $result;
    }
    
    public function updatePhong($phong){
        $sql ="UPDATE dm_phong
                SET
                ma_trungtam = '".$phong->getma_trungtam()."' 
                ,sophong = '".$phong->getsophong()."' 
                ,soluongmax =". $phong->getsoluongmax()." 
            WHERE ma_phong = '".$phong->getma_phong()."'";
        $result = mysql_query($sql) or die (mysql_error());
        return $result;
    }
    
    public function deletePhong($ma_phong){
        $isOK= $this->checkPhong($ma_phong);
        if($isOK==true){
             $sql="DELETE FROM dm_phong
             WHERE     ma_phong = '".$ma_phong."'";
             $result = mysql_query($sql) or die (mysql_error());
             return $result;
        }else{
            return "khong duoc phep xoa";
        }
       
    }
    
    function checkPhong($ma_phong){
        $sql="select count(*) from tbl_lopmon where ma_phong ='".$ma_phong."'";
        $result = mysql_query($sql) or die(mysql_error());
        if($result >0){
            return false;
        }else{
            return true;
        }        
        
    }
    
    
    function setPhong($row){
        $phong = new DmPhong();
        $phong->setPhong($row['ma_phong'],$row['ma_trungtam'],$row['soluongmax'],$row['sophong']);
        return $phong;
    }
    
}

?>